<template>

  <h3 class="mb-12px">保养记录</h3>
  <!-- 列表 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >

      <el-form-item>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['oa:vehicle-maintenance:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 保养记录
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号号" align="center" prop="id" />
      <el-table-column label="保养日期" align="center" prop="maintenanceDate" :formatter="dateFormatter2" />
      <el-table-column label="保养费用(元)" align="center" prop="maintenanceCost" />
      <el-table-column label="保养内容" align="center" prop="maintenanceContent" />
      <el-table-column label="经手人" align="center" prop="nickName" />
      <el-table-column
        label="记录时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter2"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['oa:vehicle-maintenance:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['oa:vehicle-maintenance:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->

  <VehicleMaintenanceForm
    ref="formRef"
    :vehicle-id="props.vehicleId"
    @success="getList"
  />
</template>

<script setup lang="ts">
import {  dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { VehicleMaintenanceApi, VehicleMaintenanceVO } from '@/api/oa/vehiclemaintenance'
import VehicleMaintenanceForm from '@/views/oa/vehicle/components/VehicleMaintenanceForm.vue'
import { VehicleApi } from '@/api/oa/vehicle'
import { defineProps } from 'vue'

/** 车辆保养记录主 列表 */
defineOptions({ name: 'VehicleMaintenance' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<VehicleMaintenanceVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  maintenanceDate: [],
  vehicleName: undefined,
  licensePlate: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const props = defineProps({
  vehicleId: {
    type: [Number, String],
    required: true
  }
})
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await VehicleMaintenanceApi.getVehicleMaintenancePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await VehicleMaintenanceApi.deleteVehicleMaintenance(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await VehicleMaintenanceApi.exportVehicleMaintenance(queryParams)
    download.excel(data, '车辆保养记录主.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {

  // 根据 vehicleId 获取车牌号
  try {
    const vehicleData = await VehicleApi.getVehicle(props.vehicleId)

    queryParams.licensePlate = vehicleData.plateNumber
  } catch (error) {
    console.error('获取车辆信息失败:', error)
  }

  await getList()
})
</script>
